using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace mightyParticles
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteBatch spriteBatch2;
        GraphicsDevice device;
        Texture2D backgroundTexture;
        Texture2D particle;
        int width,height;
        Vector2 pos;
        SpriteFont font;
        ParticleEngine pe;
        ParticleEngine pe2,pe3,pe4,pe5,pe6,pe7,pe8,pe9;
        GameTime currentTime;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "Mighty Particles!";
            currentTime = new GameTime();
            pe = new ParticleEngine(new Vector2(600, 400), 300, 150, 10);
            pe2 = new ParticleEngine(new Vector2(400, 350), 300, 150, 10);
            pe3 = new ParticleEngine(new Vector2(200, 400), 300, 150, 10);
            pe4 = new ParticleEngine(new Vector2(600, 200), 300, 150, 10);
            pe5 = new ParticleEngine(new Vector2(400, 250), 300, 150, 10);
            pe6 = new ParticleEngine(new Vector2(200, 200), 300, 150, 10);
            pe7 = new ParticleEngine(new Vector2(400, 300), 500, 400);
            pe8 = new ParticleEngine();
            pe9 = new ParticleEngine();
            this.IsMouseVisible = true;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteBatch2 = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            backgroundTexture = Content.Load<Texture2D>("16574214");
            particle = Content.Load<Texture2D>("Particle");
            // TODO: use this.Content to load your game content here
            width = device.PresentationParameters.BackBufferWidth;
            height = device.PresentationParameters.BackBufferHeight;
            //pe.AddExplosion(new Vector2(400, 200), 200, 5.0f, 1.0f, currentTime);
            font = Content.Load<SpriteFont>("gameFont");
            pos.X = 200;
            pos.Y = 200;
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            ProcessKeyboard();
            //if (pe.particleList.Count > 0)
                //pe.UpdateParticles(currentTime);

            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            DrawBackground();
            spriteBatch.End();
            spriteBatch2.Begin(SpriteSortMode.Deferred, BlendState.Additive);
            /*pe.DrawORing(spriteBatch2, particle);
            pe2.DrawORing(spriteBatch2, particle);
            pe3.DrawORing(spriteBatch2, particle);
            pe4.DrawORing(spriteBatch2, particle);
            pe5.DrawORing(spriteBatch2, particle);
            pe6.DrawORing(spriteBatch2, particle);*/
            MouseState mouseState = Mouse.GetState();
            Vector2 mousePos = new Vector2(mouseState.X, mouseState.Y);
            //pe7.DrawSparks(spriteBatch2, particle, mousePos);
            //pe8.Trail(spriteBatch2, particle, mousePos, 200, 1000, gameTime);
            pe8.Pulse(spriteBatch2, particle, new Vector2(-50, 200), 900, 1.5f);
            pe9.Trail(spriteBatch2, particle, mousePos, 1000, 1000, gameTime);
            spriteBatch2.End();
            base.Draw(gameTime);
        }
        private void DrawBackground()
        {
            //Rectangle screenRectangle = new Rectangle(0, 0, width, height);
            //spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
        }
        private void ProcessKeyboard()
        {
            KeyboardState keyState = Keyboard.GetState();
            
            if (keyState.IsKeyDown(Keys.Left))
            {
                pos.X -= 0.9f;
            }
            if (keyState.IsKeyDown(Keys.Right))
            {
                pos.X += 0.9f;
            }
            if (keyState.IsKeyDown(Keys.Up))
            {
                pos.Y -= 0.9f;
            }
            if (keyState.IsKeyDown(Keys.Down))
            {
                pos.Y += 0.9f;
            }
            if (keyState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
        }
    }
}
